import axios from 'axios'

// 引入Vue
import Vue from 'vue'

// 引入Vuex
import Vuex from 'vuex'

// 
Vue.use(Vuex)

const state = {
    count:0,
    price:999,
    movieList:[]
}

// 更改vuex 的store 中的状态的唯一方法是提交mutation
// mutation的函数的第一个参数就是当前的state对象
// 处理同步函数
const mutations = {
    increment(state){
        state.count++
    },
    // 
    incrementN(state,{n}){
        state.count +=n
    },

    steMovieList(state,payload){
        state.movieList = payload
    }
}

// 处理异步函数
const actions = {
    incrementWait({commit}){
        setTimeout(()=>{
            commit('increment')
        },2000)
    },
    incrementNWait({commit},payload){
        setTimeout(()=>{
            commit('incrementN',payload)
        },2000)
    },
    async getMovieList({commit}){
        const result = await axios.get('https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=2&ret_num=48&session=b9fd987164f6aa47fad266f57dffaa6a')
        console.log(result);
        commit('steMovieList',result.data.data.list)
    }
}

export default new Vuex.Store({
    state,
    mutations,
    actions
})